package com.syc.springsecuritydemo1.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.根据username查询数据库
        if (!"admin".equals(username)) {
            throw new UsernameNotFoundException("用户名或密码错误！");
        }
        //2.根据查询对象比较密码
        String password = passwordEncoder.encode("1");
        //3.返回用户对象
        return new User("admin", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,/index1.html,ROLE_admin"));
    }
}
